#include <iostream>
#include <vector>
#include "Thread.hpp"
#include <unistd.h>
#include "Mutex.hpp"

const int thread_cnt = 5;

int test_cnt = 0;
MutexModule::Mutex mutex;
pthread_mutex_t _mutex = PTHREAD_MUTEX_INITIALIZER;
void TestFunc()
{
    while (true)
    {
        // sleep(1);
        MutexModule::MutexGuard lock(mutex);
        //mutex.Lock();
        //pthread_mutex_lock(&_mutex);
        if(test_cnt != 1000)
            test_cnt++;
        std::cout << "test_cnt 被 执行" << " test_cnt= " << test_cnt << std::endl;
        if (test_cnt == 1000)
        {
            //std::cout << "线程break了" << std::endl;
            //mutex.UnLock();
            //pthread_mutex_unlock(&_mutex);
            break;
        }
        // sleep(1);
        //pthread_mutex_unlock(&_mutex);
        //mutex.UnLock();
    }
}
int main()
{
    std::vector<ThreadModule::Thread> threads;

    for (int i = 0; i < thread_cnt; i++)
    {
        threads.emplace_back(TestFunc);
    }

    for (auto &thread : threads)
    {
        thread.Start();
    }

    // sleep(2);

    for (auto &thread : threads)
    {
        thread.Join();
    }
}